package v2

type DataSourceDecorator struct {
	Wrapped DataSource
}

func (d *DataSourceDecorator) Write(s string) {
	d.Wrapped.Write(s)
}

func (d *DataSourceDecorator) Read() string {
	return d.Wrapped.Read()
}

// EncryptionDecorator 加密的装饰器
type EncryptionDecorator struct {
	DataSourceDecorator
}

func (ed *EncryptionDecorator) Write(s string) {
	ed.Wrapped.Write(ed.Encode(s))
}

func (ed *EncryptionDecorator) Read() string {
	return ed.Decode(ed.Wrapped.Read())
}

func (ed *EncryptionDecorator) Encode(data string) string {
	return data + "encoded"
}

func (ed *EncryptionDecorator) Decode(data string) string {
	return data[:len(data)-len("encoded")]
}

// 压缩的装饰器
type CompressionDecorator struct {
	DataSourceDecorator
	Level int
}

func (cd *CompressionDecorator) Write(s string) {
	cd.Wrapped.Write(cd.Compress(s))
}

func (cd *CompressionDecorator) Read() string {
	return cd.Decompress(cd.Wrapped.Read())
}

func (cd *CompressionDecorator) Compress(data string) string {
	return data + "\ncompress"
}

func (cd *CompressionDecorator) Decompress(data string) string {
	return data[:len(data)-len("\ncompress")]
}
